- image processing with Xi -
There are two major ways to store and handle images in Xi.
Colormap based image: The Image is a two dimensional array of char (char have values between 0..255). Each pixel of the image is represented by one entry of the matrix. The number of columns of the image correspond to the columns of the matrix and the rows of the image correspond to the rows of the matrix. An entry of the matrix is an index to the colormap. Each entry of the colormap contains three char values, the intensities of the colors red, green and blue. The major disadvantage is that a picture can have only 256 different colors.
( 1)>picture={ {0,0,1,0,0},{0,2,0,3,0},{4,0,0,0,4},{0,3,0,2,0},{0,0,1,0,0} }; ( 2)>colormap={ {0,0,0}, {10,10,10}, {150,0,150}, {0,150,0}, {0,0,240} };The corresponding picture looks like this
The second way is more flexible and recommended for working with Xi.
RGB color system: Each pixel of the image is represented directly by it's intensities for red, green and blue. The intensities are char values. For an image with k rows and l columns you need to have an array of dimension (k,l,3) To store the same picture as in the example above type
( 3)>rgb=carr(5,5,3); ( 4)>rgb[0,*,*]={ {0,0,0}, {0,0,0}, {10,10,10},{0,0,0}, {0,0,0} }; ( 5)>rgb[1,*,*]={ {0,0,0}, {150,0,150},{0,0,0}, {0,150,0}, {0,0,0} }; ( 6)>rgb[2,*,*]={ {0,0,240},{0,0,0}, {0,0,0}, {0,0,0}, {0,0,240} }; ( 7)>rgb[3,*,*]={ {0,0,0}, {0,150,0}, {0,0,0}, {150,0,150},{0,0,0} }; ( 8)>rgb[4,*,*]={ {0,0,0}, {0,0,0}, {10,10,10},{0,0,0}, {0,0,0} };With the RGB color system you can choose between 16.7 Mio colors but compared to a colormap you need three times more memory.
The function map2rgb converts a colormap based image to the RGB color system.
( 9)>same=map2rgb(picture, colormap);On the other hand the function rgb2map performs a conversion from the RGB color system to a colormap based image
( 10)>[picture,colormap]=rgb2map(same);If the image has more then 256 colors reduce the total number of colors using the Heckbert's median cut method.
( 11)>new=color_reduce(rgb,\colors=12);You can also enable a Floyd-Steinberg error diffusion step
( 12)>new=color_reduce(rgb,\colors=12,\floyd);Alternatively you can skip the color-choosing step by specifying your own set of colors
( 13)>newmap={ {0,0,0}, {255,255,255} }; ( 14)>newimage=color_reduce(rgb,\colormap=newmap);